package com.bigdata.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.cache.Cache.ValueWrapper;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CacheUtil {

	public static void saveToCache(String cacheName, Serializable key, Serializable value) {
		CacheManager manager = CacheManager.create();

		Cache cache = manager.getCache(cacheName);
		cache.put(new Element(key, value));
	}

	public static Object readObjectFromCache(String cacheName, Serializable key) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getObjectValue();
	}

	public static Serializable readFromCache(String cacheName, Serializable key) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		return cache.get(key).getValue();
	}

	public static void removeFromCache(String cacheName, Serializable key) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		cache.remove(key);
	}

	public static void clearCache(String cacheName) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		cache.removeAll();
	}

	public static String[] getCacheNames() {
		CacheManager manager = CacheManager.create();
		return manager.getCacheNames();
	}

	public static List getKeys(String cacheName) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		return cache.getKeys();
	}

	public static Map<String, Object> getCacheStatistics(String cacheName) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache(cacheName);
		Map<String, Object> map = new HashMap();

		map.put("cacheSize", Integer.valueOf(cache.getSize()));

		map.put("memoryStoreSize", Long.valueOf(cache.getMemoryStoreSize()));

		map.put("cacheHits", Long.valueOf(cache.getStatistics().getCacheHits()));

		map.put("cacheMisses", Long.valueOf(cache.getStatistics().getCacheMisses()));
		return map;
	}

	@SuppressWarnings("all")
	public static List<String> getCacheMethods(Class cl,
			org.springframework.cache.CacheManager cacheManager) {
		Method[] methods;
		List<String> strmethods;
		org.springframework.cache.Cache ca = cacheManager.getCache("waf");
		ValueWrapper v = ca.get(cl.getName());
		if (v != null) {
			Map<String, Object> map = (Map<String, Object>) v.get();
			strmethods = (List<String>) map.get(cl.getName());
			if (strmethods == null) {
				methods = cl.getDeclaredMethods();
				strmethods = new ArrayList<String>();
				for (Method md1 : methods) {
					strmethods.add(md1.getName());
				}
				map.put(cl.getName(), strmethods);
			}
		} else {
			methods = cl.getDeclaredMethods();
			Map<String, Object> map = new HashMap<String, Object>();
			strmethods = new ArrayList<String>();
			for (Method md1 : methods) {
				strmethods.add(md1.getName());
			}
			map.put(cl.getName(), strmethods);
			ca.put(cl.getName(), map);
		}
		return strmethods;
	}
}
